package com.panshi.util;

import com.panshi.entity.AppCommentT;
import com.panshi.entity.AppEssayT;
import com.panshi.entity.AppUserT;
import com.panshi.service.CommentClientService;
import com.panshi.service.UserClientService;
import com.panshi.service.ZxClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component
public class ZxUtils {

    @Autowired
    private ZxClientService zxClientService;

    @Autowired
    private CommentClientService commentClientService;

    @Autowired
    private UserClientService userClientService;

    public List<AppCommentT> commList(int id,int page){
        List<AppCommentT> commentTS = commentClientService.commZxId(id, page, 3);

        commentTS.forEach(item->{
            List<AppCommentT> commById = commentClientService.getCommById(item.getId());
            if(commById!=null){

                commById.forEach(data->{
                    AppUserT byid = userClientService.byid(data.getUserId());
                    if(byid!=null){
                        data.setUserT(byid);
                    }
                });
                item.setCommList(commById);
            }

            AppUserT byid = userClientService.byid(item.getUserId());
            if(byid!=null){
                item.setUserT(byid);
            }


        });
        System.out.println("用户评论信息");
        System.out.println(commentTS);
        return commentTS;
    }


    //咨询分类查询
    public List<AppEssayT> zxList(int page,int limit,String type){
        //推荐咨询
        List<AppEssayT> zxAppEssayTList = zxClientService.zxPage(page,limit,type);
        if(zxAppEssayTList!=null){
            zxAppEssayTList.forEach(essay->{
                essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
            });
        }
        return zxAppEssayTList;
    }

    public List<AppEssayT> zxPage (int page,int limit,String type){
        List<AppEssayT> zxAppEssayTList = new ArrayList<>();
        //咨询
        if(type.equals("10")){  //推荐
            zxAppEssayTList = zxClientService.zxPage(page, limit, "-1");
            Collections.shuffle(zxAppEssayTList);
        }else if(type.equals("15")){ //热度
            zxAppEssayTList = zxClientService.zxPage(page, limit, "read_count");
        }else if(type.equals("20")){//最新
            zxAppEssayTList = zxClientService.zxPage(page, limit, "-1");
        }

        zxAppEssayTList.forEach(essay->{
            essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
        });
        return zxAppEssayTList;
    }


    public List<AppEssayT> zxEssayAddClass(List<AppEssayT> zxAppEssayTList){
        if(zxAppEssayTList!=null){
            zxAppEssayTList.forEach(essay->{
                essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
            });
        }
        return zxAppEssayTList;
    }

    public List<AppEssayT> zxcPage (int cid ,int page,int limit,String type){
        List<AppEssayT> zxAppEssayTList = new ArrayList<>();
        //咨询
        if(type.equals("10")){  //推荐
            zxAppEssayTList = zxClientService.zxCPage(cid, page, limit, "-1");
            Collections.shuffle(zxAppEssayTList);
        }else if(type.equals("15")){ //热度
            zxAppEssayTList = zxClientService.zxCPage(cid, page, limit,  "read_count");
        }else if(type.equals("20")){//最新
            zxAppEssayTList = zxClientService.zxCPage(cid, page, limit, "-1");
        }

        zxAppEssayTList.forEach(essay->{
            essay.setClassifyT(zxClientService.bycid(essay.getClassifyId()));
        });
        return zxAppEssayTList;
    }


    public String zxSeek(List<AppEssayT> zxEssayt){
        String info = "";
        for (int i = 0;i<zxEssayt.size();i++){
            AppEssayT essayt = zxEssayt.get(i);
            SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
            String format = sdf.format(essayt.getGroundTime());


            info += "<div class=\"list-item\">\n" +
                    "\n" +
                    "\t<div class=\"list-item-left\">\n" +
                    "\t\t<a href=\"/consult_solo/"+essayt.getEssayId()+"\">\n" +
                    "\t\t\t<img src=\""+essayt.getImgPath()+"\" alt=\"有贝口腔网\">\n" +
                    "\t\t</a>\n" +
                    "\t</div>\n" +
                    "\t<div class=\"list-item-right\">\n" +
                    "\t\t<div class=\"list-item-title\">\n" +
                    "\t\t\t<a href=\"/consult_solo/"+essayt.getEssayId()+"\">"+essayt.getEssayTitle()+"</a>\n" +
                    "\t\t</div>\n" +
                    "\t\t<div class=\"list-item-content-text\">\n" +
                    "\t\t\t<a href=\"/consult_solo/"+essayt.getEssayId()+"\">\n" +
                    "\t\t\t"+essayt.getEssaySynopsis()+"\n" +
                    "\t\t\t</a>\n" +
                    "\t\t</div>\n" +
                    "\t\t<div class=\"list-item-info\">\n" +
                    "\t\t\t<a href=\"/zx/"+essayt.getClassifyId()+"\"><span class=\"tui-jian\" >"+essayt.getClassifyT().getClassifyName()+"</span></a>\n" +
                    "\t\t\t<div class=\"ping-lun\">\n" +
                    "\t\t\t\t<a href=\"/zx/"+essayt.getClassifyId()+"\">\n" +
                    "\t\t\t\t\t<img src=\"/icon/pinglun.png\">\n" +
                    "\t\t\t\t\t<span>"+essayt.getCommentCount()+"\n" +
                    "\t\t\t\t\t</span>评论\n" +
                    "\t\t\t\t</a>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<div class=\"shou-cang\">\n" +
                    "\t\t\t\t<a href=\"/consult_solo/"+essayt.getEssayId()+"\">\n" +
                    "\t\t\t\t\t<img src=\"/icon/shoucang.png\">\n" +
                    "\t\t\t\t\t<span>"+essayt.getCollectCount()+"\n" +
                    "\t\t\t\t\t</span>收藏\n" +
                    "\t\t\t\t</a>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<div class=\"yue-du\">\n" +
                    "\t\t\t\t<a href=\"/consult_solo/"+essayt.getEssayId()+"\">\n" +
                    "\t\t\t\t\t<img src=\"/icon/yuedu.png\">\n" +
                    "\t\t\t\t\t<span>"+essayt.getReadCount()+"</span>阅读\n" +
                    "\t\t\t\t</a>\n" +
                    "\t\t\t</div>\n" +
                    "\t\t\t<span class=\"dotted\">·</span>\n" +
                    "\t\t\t<span class=\"time-before\">"+format+"</span>\n" +
                    "\t\t</div>\n" +
                    "\t</div>\n" +
                    "</div>\n";
        }
        return info;
    }


}
